<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>方案修改</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <script src="../../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../res/js/Tools.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/xm-select.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../res/js/login_common.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        $.ajaxSetup({
            async:false
        });
    </script>
    <style type="text/css">
        .layui-input-block{
            width:300px;
        }
    </style>
</head>
<body>
<form class="layui-form" id="atsfrom">
    <input id="schemeId" name="schemeId" type="hidden"></input>

    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block" style="margin-top: 10px;">
            <input type="text" id="schemeName" name="schemeName" lay-verify="required" placeholder="请输入方案名称" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">方案年度</label>
        <div class="layui-input-block">
            <input type="text" name="year" class="layui-input" lay-verify="required" id="year" placeholder="请输入年份" autocomplete="off">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">方案月份</label>
        <div class="layui-input-block">
            <input type="number" id="month" name="month" lay-verify="required" class="layui-input" min=1 max=12 placeholder="请输入月份">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件1</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
            <input type="text" disabled readonly="readonly" id="upfileone" lay-verify="file" style="width:300px;background: #ffffff;border:none;">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件2</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test9">选择文件</button>
            <input type="text" disabled readonly="readonly" id="upfiletwo" style="width:300px;background: #ffffff;border:none">
        </div>
    </div>
        <div class="layui-form-item">
            <label class="layui-form-label">附件3</label>
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-normal" id="test10">选择文件</button>
                <input type="text" disabled readonly="readonly" id="upfilethree" style="width:300px;background: #ffffff;border:none">
            </div>
        </div>

    <div class="layui-form-item">
        <label class="layui-form-label">运动员</label>
        <div class="layui-input-block">
            <div id="demo2" class="xm-select-demo"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">说明:建议</label>
        <div class="layui-input-block">
            <textarea id="exp" name="exp" lay-verify="required" placeholder="请填写说明或建议" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        </div>
    </div>

</form>
<!--表单监听-->
<script>
    $(function(){
        $("#excelImport").click(function() {
            $('#fileupload').click();
        });
    })
    var form = null;
    var demo2 = null;
    layui.use(['form','laydate','layedit','upload'], function(){
        form = layui.form
        ,layedit = layui.layedit
        ,upload = layui.upload
        ,laydate = layui.laydate;
        //年选择器
        laydate.render({
            elem: '#year'
            ,type: 'year'
            ,trigger: 'click'
        });

        //监听提交
        form.on('submit(formDemo)', function(data){
            //获取当前多选选中的值
            var selectArr = demo2.getValue();
            //JSON.stringify(selectArr, null, 2)
            //console.info(selectArr);
            var formData = new FormData(document.getElementById("atsfrom"));
            formData.append("selectArr",JSON.stringify(selectArr, null, 2));
            var url="../../mentalityas/addorupdate";
            $.ajax({
                url:url,
                type:"post",
                data:formData,
                async:false,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){
                    if(obj.num==1){
                        layer.msg(obj.msg,{
                            icon:1,
                            time:1000 //1秒关闭(如果不配置默认三秒)
                        },function(){
                            //关闭当前层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父页面
                            parent.location.reload();
                        });
                    }else{
                        alert(obj.msg);
                    }
                }
            });
            return false;
        })
        findAtsEmpAthlete();
        findById();
        var id=getQueryString("id");
        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //如果方案ID为空 新增验证附件不能为空，否则不需要验证
        if(id==null||id===""){
        //自定义验证规则
        form.verify({
            file: function(value){
                if(value.length<=0){
                    return '附件1不能为空哦';
                }
            }
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });
    }
        upload.render({
            elem: '#test8'
            ,auto: false
            ,accept: 'file'
            ,field:'fileOne'
            ,choose: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                obj.preview(function(index, file, result){
                    $("#upfileone").val(file.name);
                });
            }
            ,done: function(res){
                console.log(res)
            }
        });
        upload.render({
            elem: '#test9'
            ,auto: false
            ,accept: 'file'
            ,field:'fileTwo'
            ,choose: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                obj.preview(function(index, file, result){
                    $("#upfiletwo").val(file.name);
                });
            }
            ,done: function(res){
                console.log(res)
            }
        });
        upload.render({
            elem: '#test10'
            ,auto: false
            ,accept: 'file'
            ,field:'fileThree'
            ,choose: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                obj.preview(function(index, file, result){
                    $("#upfilethree").val(file.name);
                });
            }
            ,done: function(res){
                console.log(res)
            }
        });
    })

    function findAtsEmpAthlete(){
        var schemeId=getQueryString("id");
        console.info("sss:"+loginObj.manager.usType+":"+loginObj.manager.usWaiid);
        var usWaiid = loginObj.manager.usWaiid;
        if(loginObj.manager.usType=="教练"){
            var url = "../../AtsEmpAthleteAction/coachAthlete";
            var data = {id:usWaiid};
            var arr=[];
            var athletearr = findByAthleteId();
            var i =0;
            var a=0;
            $.post(url,data,function(obj){
                $.each(obj,function(index,item){
                    if(schemeId==null){
                        arr.push({name:item.athleteName,value:item.athleteId});
                    }else if(schemeId!=null){
                        arr.push({name: item.athleteName, value: item.athleteId});
                        $.each(athletearr,function(indexs,items){
                            console.info(item.athleteId+"==="+athletearr[indexs]);
                            if(item.athleteId==athletearr[indexs]) {
                                arr.splice(indexs,1);
                                arr.push({name: item.athleteName, value: item.athleteId,selected: true});
                            }
                        })
                    }
                });
                //重新渲染
                form.render();
            },"json");
        }

        demo2 = xmSelect.render({
            el: '#demo2',
            toolbar: {
                show: true,
                list: [ 'ALL', 'CLEAR', 'REVERSE' ]
            },
            paging: true,
            pageSize: 3,
            autoRow: true,
            tips: '请选择运动员',
            filterable: true,
            data:arr
        })
        return arr;
    }
    //根据ID查询出所有运动员
    function findByAthleteId(){
        var id=getQueryString("id");
        var url="../../mentalityAthlete/findBySchemeId";
        var data={id:id};
        var arr=[];
        $.post(url,data,function(obj){
            $.each(obj,function(index,item){
                arr.push(item.athleteId);
                $("#exp").val(item.exp);
            })
        },"json");
        console.info("arrr:"+arr);
        return arr;
    }
    //根据方案ID查询赋值
    function findById(){
        var id = getQueryString("id");
        var url = "../../mentalityas/findBySchemeId";
        var data = {id:id};
        $.post(url,data,function(obj){
            $("#schemeId").val(obj.schemeId);
            $("#schemeName").val(obj.schemeName);
            $("#year").val(obj.year);
            $("#month").val(obj.month);
            $("#upfileone").val(obj.file1);
            $("#upfiletwo").val(obj.file2);
            $("#upfilethree").val(obj.file3);

            //重新渲染
            form.render();
        },"json");
    }

</script>
</body>
</html>